package ar.edu.itba.it.paw.web.product;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.itba.it.paw.domain.Auction;
import ar.edu.itba.it.paw.domain.Product;
import ar.edu.itba.it.paw.domain.Transaction;
import ar.edu.itba.it.paw.repo.UserRepo;
import ar.edu.itba.it.paw.web.MercadoLibre;
import ar.edu.itba.it.paw.web.MercadoLibreSession;
import ar.edu.itba.it.paw.web.base.BasePage;
import ar.edu.itba.it.paw.web.component.PriceLabel;

public class OpenAuctionsPage extends BasePage {

	@SpringBean
	UserRepo userRepo;

	public OpenAuctionsPage(){

		IModel<List<Auction>> auctionsModel = new LoadableDetachableModel<List<Auction>>() {
			@Override
			protected List<Auction> load() {
				return MercadoLibreSession.get().getUser().getPendingAuctions();
			};

		};
		add(new PropertyListView<Auction>("pendingAuctions", auctionsModel) {

			@Override
			protected void populateItem(ListItem<Auction> item) {
				item.add(new Label("name",new PropertyModel<Product>(item.getModel(), "name")).add(new SimpleAttributeModifier("style", "color: #0099FF")));
				item.add(new Label("description",new PropertyModel<Product>(item.getModel(), "description")).add(new SimpleAttributeModifier("style", "")));
				item.add(new PriceLabel("price",new PropertyMaxOfferModel(item.getModel())).add(new SimpleAttributeModifier("style", "")));
				item.add(new Link<Auction>("detail", item.getModel()) {
					@Override
					public void onClick() {
						setResponsePage(new ProductDetailPage(getModelObject()));
					}
				}.add(new Image("detail_image", MercadoLibre.DETAIL_ICON).add(new SimpleAttributeModifier("height","15px"))));
				item.add(new Link<Auction>("close", item.getModel()) {
					@Override
					public void onClick() {
						MercadoLibreSession session = MercadoLibreSession.get();
						getModelObject().setEnded(true);
						Auction prod = getModelObject();
						Transaction trans = new Transaction(prod.getUserBestOffer(),session.getUser(), prod.getQuantity(), new Date(), prod);
						prod.addTransaction(trans);
						setResponsePage(new OpenAuctionsPage());
					}
				}.add(new Image("close_image", MercadoLibre.DELETE_ICON)));
			}
		});
	}
	public class PropertyMaxOfferModel extends AbstractReadOnlyModel<Double>{

		IModel<Auction> auction;

		public PropertyMaxOfferModel(IModel<Auction> auction){
			this.auction = auction;
		}

		@Override
		public Double getObject() {
			return Double.valueOf(auction.getObject().getPrice());
		}

	}
}	
